package com.simulation.web.controller.tech;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.simulation.common.core.domain.AjaxResult;
import com.simulation.web.domain.TechTaskSubmissions;
import com.simulation.web.dto.TaskSubmissionsDTO;
import com.simulation.web.mapper.CollectionPostMapper;
import com.simulation.web.service.CollectionPostService;
import com.simulation.web.service.TaskSubmissionsService;
import com.simulation.web.vo.TechTaskSubmissionsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 唐雄杰
 * @Date 2024-08-26 19:52
 **/
@RestController
@RequestMapping("/taskSubmissions")
@Api("任务明细")
public class TaskSubmissionsController {
    @Resource
    private TaskSubmissionsService taskSubmissionsService;

    @Resource
    private CollectionPostMapper collectionPostMapper;

    @Value("${oss.bucketName}")
    private String bucketName;

    /**
     * 批量生成任务提交数据
     * @param taskSubmissions
     * @return
     */
    @PostMapping("batchInsert")
    public AjaxResult batchInsert(@RequestBody List<TechTaskSubmissions> taskSubmissions) {
        taskSubmissionsService.batchInsert(taskSubmissions, bucketName);
        return AjaxResult.success();
    }

    /**
     * 根据任务id分页查询任务提交列表
     * @param taskSubmissionsDTO
     * @return
     */
    @GetMapping("getPageByTaskId")
    public AjaxResult getPageByTaskId(TaskSubmissionsDTO taskSubmissionsDTO) {
        return AjaxResult.success(taskSubmissionsService.getPageByTaskId(taskSubmissionsDTO));
    }

    /**
     * 根据任务提交id查询提交详情
     * @param taskSubmissionsDTO
     * @return
     */
    @GetMapping("getById")
    @ApiOperation("根据任务提交id查询提交详情")
    public AjaxResult getById(TaskSubmissionsDTO taskSubmissionsDTO) {
        return AjaxResult.success(taskSubmissionsService.getById(taskSubmissionsDTO.getId()));
    }

    /**
     * 根据任务提交id删除数据
     * @param taskSubmissionsDTO
     * @return
     */
    @PostMapping("deleteById")
    public AjaxResult deleteById(@RequestBody TaskSubmissionsDTO taskSubmissionsDTO) {
        taskSubmissionsService.deleteById(taskSubmissionsDTO.getId());
        return AjaxResult.success();
    }

    /**
     * 更新提交详情（忽略null值）
     * @param taskSubmissionsDTO
     * @return
     */
    @PostMapping("updateIgnoreNull")
    @ApiOperation("更新提交详情")
    public AjaxResult updateIgnoreNull(@RequestBody TaskSubmissionsDTO taskSubmissionsDTO) {
        TechTaskSubmissions techTaskSubmission = new TechTaskSubmissions();
        BeanUtil.copyProperties(taskSubmissionsDTO, techTaskSubmission);
        taskSubmissionsService.updateIgnoreNull(techTaskSubmission);
        // 集合
        if ("collection".equals(taskSubmissionsDTO.getRelatedType()) && CollUtil.isNotEmpty(taskSubmissionsDTO.getPostIds())) {
            collectionPostMapper.deleteByCollectionId(taskSubmissionsDTO.getRelatedId());
            collectionPostMapper.insert(taskSubmissionsDTO.getRelatedId(), taskSubmissionsDTO.getPostIds());
        }
        return AjaxResult.success();
    }

    /**
     * 分页查询待完成的任务
     * @param taskSubmissionsDTO
     * @return
     */
    @GetMapping("getPageByUserId")
    public AjaxResult getPageByUserId(TaskSubmissionsDTO taskSubmissionsDTO) {
        return AjaxResult.success(taskSubmissionsService.getPageByUserId(taskSubmissionsDTO));
    }

    /**
     *
     * @return
     */
    @GetMapping("updateGrade")
    @ApiOperation("打分")
    public AjaxResult updateGrade(@RequestParam("id") Long id,@RequestParam("grade") String grade ) {
        TechTaskSubmissionsVO techTaskSubmissionsVO = taskSubmissionsService.getById(id);
        techTaskSubmissionsVO.setGrade(grade);
        TechTaskSubmissions techTaskSubmission = new TechTaskSubmissions();
        BeanUtil.copyProperties(techTaskSubmissionsVO, techTaskSubmission);
        taskSubmissionsService.updateIgnoreNull(techTaskSubmission);
        return AjaxResult.success();
    }
}
